*******5 Year Analysis*************************************************
***********************************************************************
***********************************************************************
***********************************************************************
***********************************************************************
***********************************************************************
***********************************************************************

use "$LocalData\BartikAnalyticData_2digit.dta", clear

* Set panel variables
xtset geography year


		keep if year>2002  //Restricts to period of analysis
		
		levelsof year, local(y) //Creates a macro list of all years
			
		qui tab year, gen(year_) //Generates year dummies
		drop year_1
		qui tab geography, gen(geography_) //Generates year dummies
		drop geography_1
		


egen minci = rowmin(c_i_share*)
egen nzeros = anycount(c_i_share*), value(0)


* Set panel variables
xtset geography year

* Generate income and deposit growth
gen gincome = ln(income) - ln(L.income)
label var gincome "Growth in Income"
gen gdep 	= ln(cnty_dep) - ln(L.cnty_dep)
label var gdep "Growth in Deposits"

* Define control variables
local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000 
local controls2 ln_pop_2000, unem_2000, urban_2000, lnincome2000, lnbr2000, small_dep_share2000, gSmallFirm5 
gen miss =  missing(`controls2')
*keep if miss==0



/**********************/
/* Baseline Regressions */
/**********************/
noi display "Starting baseline regressions."
noi display " "

*Deposits
noi display "Deposit regressions."
local years year>2002 & abs(gsmall_dep1)<5 & nzeros<16
*& L.small_dep_share>0 & L.small_br_share>0
*local controls ln_pop_2000 unem_2000 urban_2000 lnincome2000 lnbr2000 small_dep_share2000 

gen dlngdp5 = lngdp - L5.lngdp
gen pop_growth5 = ln_pop - L5.ln_pop
gen unem5 = unem - L5.unem
gen hpi5 = [(1+annualchange/100)*(1+L.annualchange/100)*(1+L2.annualchange/100)*(1+L3.annualchange/100)*(1+L4.annualchange/100)]^0.2
local controls 	dlngdp5 unem5 pop_growth5 hpi5 

eststo: areg gSmallFirm1 bartikGS1 L.bartikGS1 L2.bartikGS1 L3.bartikGS1 L4.bartikGS1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable5_RFS", tex replace ///
	ctitle(Small Dep Gr) ///
	addtext(REG, OLS, YEAR FE, YES, COUNTY FE, YES, CONTROLS, NO, YRS, 2006-2017)

eststo: areg gSmallFirm1 L.gSmallFirm1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable5_RFS", tex append ///
	ctitle(Small Dep Gr) ///
	addtext(REG, OLS, YEAR FE, YES, COUNTY FE, YES, CONTROLS, NO,  YRS, 2003-2017)
	
eststo: areg bartikGS1 L.bartikGS1 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable5_RFS", tex append ///
	ctitle(Bartik1) ///
	addtext(REG, OLS, YEAR FE, YES, COUNTY FE, YES, CONTROLS, NO,  YRS, 2003-2017)

local years  (year==2007 | year==2012 | year==2017) & abs(gsmall_dep5)<5 & nzeros<16

eststo: areg gsmall_dep5 gSmallFirm5 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable5_RFS", tex append ///
	ctitle(Small Dep Gr5) ///
	addtext(REG, OLS, YEAR FE, YES, COUNTY FE, YES, CONTROLS, NO,  YRS, 2003-2017)


eststo: reg gsmall_dep5 gSmallFirm5 `controls' i.year if `years', vce(cluster cluster3)
outreg2 using "$LocalData\Tables\BartikRegTable5_RFS", tex append ///
	ctitle(Small Dep Gr5) ///
	addtext(REG, OLS, YEAR FE, YES, COUNTY FE, NO, CONTROLS, YES,  YRS, 2003-2017)

eststo: areg gsmall_dep5 gSmallFirm5 gLargeFirm5 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable5_RFS", tex append ///
	ctitle(Small Dep Gr5) ///
	addtext(REG, OLS, YEAR FE, YES, COUNTY FE, YES, YRS, 2006-2017)

local years  (year==2007 | year==2012 | year==2017) & abs(gsmall_dep5)<5  & nzeros<16
eststo: xtivreg2 gsmall_dep5 year_* (gSmallFirm5 = bartikGS5)  if `years', fe cluster(cluster3)
gen sample = e(sample)
bysort geography: egen no_singles = sum(sample)



local years  sample==1 & no_singles>1
eststo: areg gSmallFirm5  bartikGS5 i.year if `years', vce(cluster cluster3) absorb(geography)
outreg2 using "$LocalData\Tables\BartikRegTable5_RFS", tex append ///
	ctitle(gSmallFirm5) ///
	addtext(REG, OLS, YEAR FE, YES, COUNTY FE, YES, CONTROLS, YES,  YRS, 2003-2017)

capture file close fh
file open fh  using "$LocalData\Tables\Adao_Standard_Errors_5yearsRFS.tex", write replace
file write fh "\toprule" _n
file write fh  " (1) & (2)  & (3)  & (4)  & (5) & (6) & (7) & (8) " _n

	
	preserve
	drop geography_* year_*
	local years (year==2007 | year==2012 | year==2017) & abs(gsmall_dep5)<5  & nzeros<16 & !missing(gSmallFirm5)
	
		keep if `years'
		levelsof year, local(y) //Creates a macro list of all years
		qui tab geography, gen(geography_) //Creates a macro list of all years
		qui tab year, gen(year_) //Creates a macro list of all years
		drop year_1 geography_1
		
			foreach year in `y' {
			foreach ind_var of varlist c_i_share_2000* {
				qui gen t`year'_`ind_var' = `ind_var' * (year == `year')
				}
			}

		
		local controls2 year_* geography_* 
		local weightmatrix "t*_c_i_share_2000*"
		local filter `years'
		
		noi Adao gsmall_dep5 bartikGS5 `controls2' if `filter', wmatrix(`weightmatrix') tsls(gSmallFirm5)

/*		local se7 = string(se0[1,1], "%9.3f")
		local ciL7 = string(ciL[1,1], "%9.3f")
		local ciH7 = string(ciH[1,1], "%9.3f")
	*/	
		local se8 =string(se_alpha[1,1], "%9.3f")
		local ciL8 =string(ciL2SLS[1,1], "%9.3f")
		local ciH8 =string(ciH2SLS[1,1], "%9.3f")

	restore


file write fh  "`se1' & `se2' & `se3' & `se4' & `se5' & `se6' & `se7' & `se8'   \\" _n
file write fh  " & [`ciL1', `ciH1'] & [`ciL2', `ciH2'] & [`ciL3', `ciH3'] & [`ciL4', `ciH4'] & [`ciL5', `ciH5'] & [`ciL6', `ciH6'] & [`ciL7', `ciH7'] & [`ciL8', `ciH8']    \\" _n
	
	
local years  (year==2007 | year==2012 | year==2017) & abs(gsmall_dep5)<5  & nzeros<16
eststo: xtivreg2 gsmall_dep5 year_* (gSmallFirm5 = bartikGS5)  if `years', fe cluster(cluster3)
local widstat = round(`e(widstat)',0.1)
outreg2 using "$LocalData\Tables\BartikRegTable5_RFS", tex append ///
	ctitle(Small Dep Gr5) ///
	addtext(REG, OLS, YEAR FE, YES, COUNTY FE, YES, F-stat, `widstat', YRS, 2007-2017)

	
file close fh
